﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Nop.Core;
using Nop.Core.Domain;
using Nop.Core.Domain.Blogs;
using Nop.Core.Domain.Catalog;
using Nop.Core.Domain.Common;
using Nop.Core.Domain.Customers;
using Nop.Core.Domain.Forums;
using Nop.Core.Domain.Localization;
using Nop.Core.Domain.Messages;
using Nop.Core.Domain.Orders;
using Nop.Core.Domain.Tax;
using Nop.Services.Catalog;
using Nop.Services.Common;
using Nop.Services.Customers;
using Nop.Services.Directory;
using Nop.Services.Forums;
using Nop.Services.Localization;
using Nop.Services.Messages;
using Nop.Services.Orders;
using Nop.Services.Security;
using Nop.Services.Seo;
using Nop.Services.Topics;
using Nop.Web.Extensions;
using Nop.Web.Framework.Localization;
using Nop.Web.Framework.Security;
using Nop.Web.Framework.Themes;
using Nop.Web.Framework.UI.Captcha;
using Nop.Web.Models.Catalog;
using Nop.Web.Models.Common;

namespace Nop.Web.Controllers
{
    public partial class CommonController : BaseNopController
    {

        //menu
        [ChildActionOnly]
        public ActionResult Menu()
        {
            var model = new EcrMenuModel()
            {
                RecentlyAddedProductsEnabled = _catalogSettings.RecentlyAddedProductsEnabled,
                BlogEnabled = _blogSettings.Enabled,
                ForumEnabled = _forumSettings.ForumsEnabled
            };
            var cats = _categoryService.GetAllCategories(false);

            if (cats.Count > 0)
            {
                foreach (var cat in cats.Where(x => x.Deleted == false))
                {
                    model.Categories.Add(new EcrMenuCategoryModel()
                    {
                        Id = cat.Id,
                        Name = cat.GetLocalized(x => x.Name),
                        SeName = cat.GetSeName(),
                    });


                    var cat1 = model.Categories.Where(x => x.Id == cat.Id).FirstOrDefault();

                        var subcats = _categoryService.GetAllCategoriesByParentCategoryId(cat1.Id, false);

                        if (subcats.Count > 0)
                        {
                            foreach (var subcat in subcats.Where(x => x.Deleted == false))
                            {
                                cat1.SubCategories.Add(new EcrMenuCategoryModel()
                                {
                                    Id = subcat.Id,
                                    Name = subcat.GetLocalized(x => x.Name),
                                    SeName = subcat.GetSeName(),
                                });
                            }
                        }
                        else
                        {
                            var products = _productService.GetProductsByCategoryId(cat1.Id);

                            if (products.Count > 0)
                            {
                                foreach (var prod in products.Where(x => x.Deleted == false))
                                {
                                    cat1.Products.Add(new EcrMenuProductModel()
                                    {
                                        Id = prod.Id,
                                        Name = prod.Name,
                                        SeName = prod.GetSeName(),
                                        ShortDescription = prod.ShortDescription
                                    });
                                }
                            }

                        }

                    }

            }

            return PartialView(model);
        }

    }
}
